package com.gowhere.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MapperUtil {

    private MapperUtil(){} ;

    private static SqlSessionFactory factory = null ;

    static {
        //1.读取mybatis核心配置信息
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("sql.xml");
            //2.构建SQLSessionFactory对象
             factory = new SqlSessionFactoryBuilder().build(is);
            //3.获取SqlSession对象
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取SqlSession对象
    public static SqlSession getSession() {
        return factory.openSession();
    }

    //释放资源
    public static void close(SqlSession session){
        //5.提交事务
        session.commit();
        //6.释放资源
        session.close();
    }

}
